<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="选择冷铁弹窗"
    style="min-width: 1200px"
    draggable
    @close="closeDialog"
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="冷铁名称">
          <el-input
            v-model="searchParam.name"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <div class="formTable">
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          max-height="400"
          highlight-current-row
          @current-change="handleCurrentChange1"
          border
        >
          <el-table-column
            label="冷铁名称"
            prop="name"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="尺寸mm"
            prop="size"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="重量/kg"
            prop="weight"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="图片"
            prop="img"
            min-width="100"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <div class="titleBox" v-if="scope.row.img">
                <div class="demo-image__preview img">
                  <el-image
                    style="width: 60px; height: 40px"
                    :src="`${actionUrl}/system/fileCenter/preview/${scope.row.img}`"
                    :preview-src-list="[
                    `${actionUrl}/system/fileCenter/preview/${scope.row.img}`,
                    ]"
                    fit="cover"
                    preview-teleported
                  />
                </div>
              </div>
              <div style="width: 60px; height: 40px" v-else></div>
            </template>
          </el-table-column>
          <el-table-column
            label="安全库存"
            prop="safetyStock"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="使用工序"
            prop="usageProcess"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="装载数量+3%"
            prop="loadingQuantity"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="保质期/天"
            prop="qualityGuaranteePeriod"
            min-width="100"
            show-overflow-tooltip
          />
        </el-table>
        <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import request from "@/utils/request";
defineOptions({
  name: "MESIRONSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  brekList: {
    type: Object,
    default: () => {
      return {};
    },
  },
  plantId: {
    type: String,
    default: () => {
      return "";
    },
  },
});
const multipleTable = ref();
// 图片地址
const actionUrl = import.meta.env.VITE_APP_BASE_API;
//获取表格数据 api 方法
const requestApi = "/mes/mesironbaseinfo/queryByPage";
//获取数据初始化参数
const initParam = {};
const searchParamInit = {
  name: "",
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any, parentIndex = "") => {
  return data;
};

// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  async (newVal: boolean) => {
    visibleDialog.value = newVal;
    searchParam.value.plantId = props.plantId;
    getTableList();
  }
);

// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  reset();
}
/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择物料");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const resetHandle = () => {
  Object.assign(searchParam.value, searchParamInit);
  searchParam.value.plantId = props.plantId;
  getTableList();
};
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
